feat(onnx): add input_check flag to LightningModule.to_onnx#21716
Open
nuemaan wants to merge 1 commit into
Open
feat(onnx): add input_check flag to LightningModule.to_onnx#21716nuemaan wants to merge 1 commit into
nuemaan wants to merge 1 commit into
Conversation
Optional onnx.checker.check_model pass after export, gated behind input_check=False to keep the default path unchanged. Validates the saved file via onnx.load (or onnx.load_model_from_string for BytesIO) and surfaces ValidationError to the caller. Disallowed with dynamo=True since that path returns an ONNXProgram and does not always produce a standalone protobuf file the checker can load; also rejected when file_path is None. Refs Lightning-AI#7279.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds an
input_check: bool = Falseargument toLightningModule.to_onnx. When set, aftertorch.onnx.exportreturns we load the saved model and runonnx.checker.check_modelon it, so the caller gets aValidationErrorinstead of finding out at deploy time that the exported protobuf is malformed.Default behavior is unchanged:
input_check=False.Refs #7279.
Notes on scope
The original issue lists two things: the spec check (
onnx.checker.check_model) and a PyTorch-vs-onnxruntime output comparison. I only did the first one here — the runtime comparison needsonnxruntimeas a hard dep and a way to deal with multi-output / dict-output models, which feels like a separate PR. Happy to add it as a follow-up if you'd like.Behavior
file_pathis astr/Path→ loaded withonnx.load, then checked.file_pathisBytesIO→ loaded withonnx.load_model_from_string; the buffer's cursor position is restored so the caller sees the same state as without the flag.file_path is None→ raisesValueError; there is nothing to load.dynamo=True→ raisesValueError; that path returns anONNXProgramrather than a standalone protobuf file we can hand to the checker.Tests
Added to
tests/tests_pytorch/models/test_onnx.py:test_input_check_runs_onnx_checker— happy path for both file andBytesIO.test_input_check_raises_without_file_path—ValueErrorwhenfile_path=None.test_input_check_detects_invalid_model— monkeypatchesonnx.checker.check_modelto fail; verifies the error propagates.test_input_check_rejects_dynamo—ValueErrorwhen combined withdynamo=True.All onnx tests pass locally on macOS (
torch 2.12.0,onnx 1.21.0,onnxruntime 1.26.0):Ruff lint + format pass.
Before submitting
to_onnx()method #7279docs/source-pytorch/deploy/production_advanced.rst)False.PR review
Anyone in the community is welcome to review the PR.
📚 Documentation preview 📚: https://pytorch-lightning--21716.org.readthedocs.build/en/21716/